#include "ccamerastatemonitor.h"

CCameraStateMonitor::CCameraStateMonitor(IVSModel *_ivsModel):
    IVSControllerBase(_ivsModel)
{
    currentState = lastState = INVALID;
}

void CCameraStateMonitor::run()
{
    if(initialize())
    {
INITIALIZED:
        int FPS = fps + ivsModel->CameraChecksSettings().videoPlaySpeedBoost;
        IplImage* frame = cvQueryFrame(capture);
        char key;
        isRunning = true;
        qWarning(qPrintable(QString("fps: %1").arg(FPS)));

        /* init */
        videoPlaySpeedBoost = 4;
        IplImage* grayFrame  = cvCreateImage(size, IPL_DEPTH_8U, 1);
        IplImage* runningAvg = cvCreateImage(size, IPL_DEPTH_32F, 1);
        IplImage* background = cvCreateImage(size, IPL_DEPTH_8U, 1);
        IplImage* diffImage  = cvCreateImage(size, IPL_DEPTH_8U, 1);

        cvCvtColor(frame, grayFrame, CV_RGB2GRAY);
        cvCopy(grayFrame, background);
        cvCvtScale(grayFrame, runningAvg);

        int nInitBGFrame = 0;
        int count = 0;
        while(frame && isRunning){
            cvCvtColor(frame, grayFrame, CV_RGB2GRAY);

            if(nInitBGFrame++ < ivsModel->CameraChecksSettings().noFrameToInitBackground) {
                cvRunningAvg(grayFrame,
                             runningAvg,
                             1.0/ivsModel->CameraChecksSettings().noFrameToInitBackground);
            }else{
                if(currentState != BE_COVERED){
                    cvRunningAvg(grayFrame,
                                 runningAvg,
                                 ivsModel->CameraChecksSettings().alpha);
                    cvCvtScale(runningAvg, background);
                }
                cvAbsDiff(grayFrame, background, diffImage);

                showDebugImage(background, "background");
                showDebugImage(diffImage, "difference image");

                if(cvAvg(diffImage).val[0] > ivsModel->CameraChecksSettings().beCoveredThreshold){
                    currentState = BE_COVERED;
                }else
                    currentState = NORMAL;

                showMainImage(frame);

                if(currentState !=  lastState){
                    if(count++ > ivsModel->CameraChecksSettings().stateChangeDelay){
                        if(currentState == BE_COVERED)
                            emit stateChanged("Be covered", Qt::red);
                        else if(currentState == NORMAL)
                            emit stateChanged("Ok", Qt::blue);

                        lastState = currentState;
                        count = 0;
                    }
                }else
                    count = 0;
            }

            key = cvWaitKey(1000/FPS);

            frame = cvQueryFrame(capture);
        }
        qWarning("stoped");

        /* release */
        cvReleaseImage(&grayFrame);
        cvReleaseImage(&runningAvg);
        cvReleaseImage(&background);
        cvReleaseImage(&diffImage);
    }else
    {
        emit stateChanged("No signal", Qt::red);
        while (isRunning) {
            cvWaitKey(1000);
            if(initialize())
            {
                emit stateChanged("ok", Qt::blue);
                goto INITIALIZED;
            }
        }
    }
}

void CCameraStateMonitor::stop()
{
    isRunning = false;
    currentState = INVALID;
    release();
}
